package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.CustomPopupWindow;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.di.component.DaggerHousePublisInfoComponent;
import com.kmproject.haifu.mvp.contract.HousePublisInfoContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseInfoTitleBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePublishResultBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HousePublicBean;
import com.kmproject.haifu.mvp.presenter.HousePublisInfoPresenter;
import com.kmproject.haifu.mvp.ui.adapter.HousePublicAdapter;

import org.buffer.android.buffertextinputlayout.BufferTextInputLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/01/2019 15:42
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class HousePublisInfoActivity extends BaseActivity<HousePublisInfoPresenter> implements HousePublisInfoContract.View {


    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.housePublisInfo_RecyclerView)
    RecyclerView housePublisInfoRecyclerView;
    @BindView(R.id.housePublisInfo_edit)
    EditText housePublisInfoEdit;
//    @BindView(R.id.housePublisInfo_BufferTextInputLayout)
//    BufferTextInputLayout housePublisInfoBufferTextInputLayout;
    @BindView(R.id.housePublisInfo_public_btn)
    TextView housePublisInfoPublicBtn;
    @BindView(R.id.housePublisInfo_txt_num)
    TextView housePublisInfo_txt_num;

    private HousePublicAdapter housePublicAdapter;
    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerHousePublisInfoComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_house_publis_info; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
     ArmsUtils.configRecyclerView(housePublisInfoRecyclerView,new LinearLayoutManager(this));
        housePublisInfoEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                  if (s.length()<601)
                  {
                      housePublisInfo_txt_num.setText(s.length()+"/600");
                  }else {
                      housePublisInfo_txt_num.setText(s.toString().substring(0,600));
                  }
            }
        });
    }
    @Override
    public String reSetTitle() {
        if ("zz".equals(getIntent().getStringExtra("type"))) {
            mPresenter.dictListToConfiguration(Api.HOUSE_RENT_DETAIL_ZZ);
            return ArmsUtils.getString(this, R.string.house_publis_selecttype_item_zz);
        } else if ("dz".equals(getIntent().getStringExtra("type"))) {
            mPresenter.dictListToConfiguration(Api.HOUSE_RENT_DETAIL_DZ);
            return ArmsUtils.getString(this, R.string.housepublishpublish_info_title_dz);
        } else if ("hz".equals(getIntent().getStringExtra("type"))){
            mPresenter.dictListToConfiguration(Api.HOUSE_RENT_DETAIL_HZ);
            return ArmsUtils.getString(this, R.string.house_publis_selecttype_item_hz);
        }else {
            mPresenter.dictListToConfiguration(Api.HOUSE_RENT_DETAIL_BS);
            return ArmsUtils.getString(this, R.string.housepublishpublish_info_title_bs);
        }

    }
    @Override
    public void showLoading() {
      loadingDialog.show();
    }

    @Override
    public void hideLoading() {
       loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void dictListToConfigurationSuccess(List<HouseInfoTitleBean.HouseInfoConfiguration> houseInfoConfigurations) {
        housePublicAdapter=new HousePublicAdapter(mPresenter.getData(houseInfoConfigurations));
        housePublisInfoRecyclerView.setAdapter(housePublicAdapter);
    }

    @Override
    public void addHouseRentSuccess() {
//        custom_popuwindow_dialog
        CustomPopupWindow customPopupWindow=   AppUtil.showPopuWindow(this, CustomPopupWindow.inflateView(this, R.layout.custom_popuwindow_dialog),housePublisInfoPublicBtn);
        View view=customPopupWindow.getContentView();
        TextView membercenter_popuwindow_dialog_txt1=view.findViewById(R.id.membercenter_popuwindow_dialog_txt1);
        TextView membercenter_popuwindow_dialog_txt2=view.findViewById(R.id.membercenter_popuwindow_dialog_txt2);
        membercenter_popuwindow_dialog_txt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                setResult(RESULT_OK,intent);
                killMyself();
            }
        });
        membercenter_popuwindow_dialog_txt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent();
                intent.putExtra("isGoToShow", 1);
                intent.putExtra("type",0);//跳转到个人中心我的发布
                setResult(RESULT_OK,intent);
                killMyself();

            }
        });

    }

    @Override
    public void updateHouseRentSuccess() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.housePublisInfo_public_btn)
    public void onClick() {
        HousePublicBean housePublicBean=(HousePublicBean)getIntent().getSerializableExtra("HousePublicBean");
        housePublicBean.setHouseRentDetail(mPresenter.getHouseConfig(housePublicAdapter.getData()));
        housePublicBean.setDetail(housePublisInfoEdit.getText().toString());
       mPresenter.addHouseRent(mPresenter.entityToMap(housePublicBean));
    }
}
